package com.wsh.controller.sys;

import com.wsh.dto.sys.DictDto;
import com.wsh.utils.JsonResult;
import com.wsh.base.BaseVo;
import com.wsh.entity.sys.DictEntity;
import com.wsh.sys.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 王思涵
 * @Date 2019/8/10
 * @Version 1.0
 * @Desciption 字典数据接口层
 **/
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "系统：子字典管理")
public class DictController {

    @Autowired
    private DictService dictService;

    @PreAuthorize("@el.check('dict:list')")
    @GetMapping("/list")
    @ApiOperation("查询子字典列表")
    public Object list(DictDto dictDto) {
        return JsonResult.success(dictService.getList(dictDto));
    }

    @GetMapping("/dictByType")
    @ApiOperation("获取字典通用方法")
    public Object dictByType(@RequestParam String type) {
        return JsonResult.success(dictService.getDictByType(type));
    }

    @PostMapping("/saveDict")
    @PreAuthorize("@el.check('dict:save')")
    @ApiOperation("保存子字典数据")
    public Object saveDict(@RequestBody DictEntity dictEntity) {
        return JsonResult.success(dictService.saveOrUpdate(dictEntity));
    }

    @PreAuthorize("@el.check('dict:del')")
    @PostMapping("/delete")
    @ApiOperation("删除子字典")
    public Object delete(@RequestParam String id) { return JsonResult.success(dictService.removeById(id)); }

    @GetMapping("/getById")
    @ApiOperation("根据主键查询")
    public Object getById(String formId) {
        return JsonResult.success(new BaseVo(dictService.getById(formId)));
    }

    @GetMapping("/getDictByKeys")
    @ApiOperation("根据类型数组获取字典数组集合")
    public Object getDictByKeys(@RequestParam String keys) {
        String[] types = keys.split(",");
        return JsonResult.success(dictService.getDIctByTypes(types));
    }

    @GetMapping("/checkChildren")
    @PreAuthorize("@el.check('dict:checkChildren')")
    @ApiOperation("查询当前字典是否存在子节点")
    public Object checkChildren(String formId) {
        return JsonResult.success(dictService.checkChildren(formId));
    }

}
